package com.wx.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.wx.entity.SysMenu;
import com.wx.entity.SysMenuExample;
import com.wx.entity.SysRole;
import com.wx.entity.SysRoleExample;
import com.wx.entity.SysUserMenu;
import com.wx.entity.SysUserMenuExample;
import com.wx.entity.SysUserRole;
import com.wx.entity.SysUserRoleExample;
import com.wx.service.SysMenuService;
import com.wx.service.SysRoleMenuService;
import com.wx.service.SysRoleService;
import com.wx.service.SysUserMenuService;
import com.wx.service.SysUserRoleService;
import com.zyht.common.AbsManageBaseController;
import com.zyht.common.bean.PropertiesUtilBean.PropertiesUtils;
import com.zyht.common.util.GsonUtil;
import com.zyht.common.util.Page;
import com.zyht.website.service.SysUserService;

/**
 * <b>描述：</b> 系统用户管理<br/>
 * <b>作者：</b>zrb <br/>
 * <b>修改日期：</b>2016年1月8日 - 上午11:06:04<br/>
 */
@Controller
@RequestMapping("admin/user")
public class SysUserController extends AbsManageBaseController {

	@Resource
	private SysMenuService sysMenuService;
	
	@Resource
	private SysRoleService sysRoleService;

	@Resource
	private SysUserMenuService sysUserMenuService;

	@Resource
	private SysRoleMenuService sysRoleMenuService;

	@Resource
	private SysUserService sysUserService;

	@Resource
	private SysUserRoleService sysUserRoleService;

	private final String ROLE_STATUS_STOP="0";
	
	private final String ROLE_STATUS_USING="1";
	
	private final String ROLE_STATUS_SYSTEM="99";
	
	private final String USER_STATUS_SYSTEM="9";
	
	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年1月11日 - 下午3:14:55<br/>
	 * <b>功能说明：</b>可用角色列表	<br/>
	 * @param currentPage
	 * @param id
	 * @param roleName
	 * @param model
	 * @return
	 */
	@RequestMapping("authorize")
	public String authorize(Integer currentPage, Integer id, String roleName, Model model){
		if(null==currentPage){
			currentPage = 1;
		}
		SysRoleExample example = new SysRoleExample();
		com.wx.entity.SysRoleExample.Criteria criteria = example.createCriteria();
		criteria.andRoleStatusEqualTo(ROLE_STATUS_USING);
		if(!StringUtils.isEmpty(roleName)){
			criteria.andRoleNameLike("%"+roleName+"%");
		}
		String pSize = PropertiesUtils.getConfig("paganation_page_size");
		int pageSize = Integer.valueOf(pSize);
		PageHelper.startPage(currentPage, pageSize);
		com.github.pagehelper.Page<SysRole> pageInfo = (com.github.pagehelper.Page<SysRole>) this.sysRoleService.list(example);
		Page<SysRole> page = new Page<SysRole>((int)pageInfo.getTotal(), currentPage, pageSize);
		page.setRecordList(pageInfo);
		model.addAttribute("page", page);
		model.addAttribute("roleName", roleName);
		
		SysUserRoleExample roleEx = new SysUserRoleExample();
		roleEx.createCriteria().andUserIdEqualTo(id);
		List<SysUserRole> urList = this.sysUserRoleService.list(roleEx);
		if(urList.size()>0){
			SysUserRole ur = urList.get(0);
			model.addAttribute("ur",ur);
		}
		model.addAttribute("uid",id);
		return "sys/role/choose_role";
	}
	
	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年1月11日 - 下午3:14:36<br/>
	 * <b>功能说明：</b>授予用户角色<br/>
	 * @param id
	 * @param uid
	 * @param model
	 * @return
	 */
	@RequestMapping("grant")
	public String grant(Integer roleid, Integer uid, Model model){
		if(null!=roleid){
			SysUserRoleExample example = new SysUserRoleExample();
			example.createCriteria().andUserIdEqualTo(uid);
			List<SysUserRole> list = this.sysUserRoleService.list(example);
			if(list.size()>0){
				SysUserRole ur = list.get(0);
				ur.setRoleId(roleid);
				this.sysUserRoleService.updateById(ur);
			}else{
				SysUserRole ur = new SysUserRole();
				ur.setRoleId(roleid);
				ur.setUserId(uid);
				this.sysUserRoleService.addOne(ur);
			}
		}
		return "redirect:/admin/user/msg";
	}
	
	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年1月11日 - 下午5:32:43<br/>
	 * <b>功能说明：</b>用户权限<br/>
	 * @param rid
	 * @param model
	 * @return
	 */
	@RequestMapping("fun")
	public String functionTree(Integer uid, Model model){
		SysUserMenuExample rmEx = new SysUserMenuExample();
		rmEx.createCriteria().andUserIdEqualTo(uid);
		List<SysUserMenu> rmList = this.sysUserMenuService.list(rmEx);
		Map<Integer, Integer> rmMap = new HashMap<Integer, Integer>();
		for(SysUserMenu rm : rmList){
			rmMap.put(rm.getMenuId(),rm.getMenuId());
		}
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andMenuShowNotEqualTo(ROLE_STATUS_STOP);
		List<SysMenu> list = this.sysMenuService.list(example);
		for(SysMenu menu : list){
			menu.setOpen(true);
			if(rmMap.containsKey(menu.getId())){
				menu.setChecked(true);
			}
		}
		Gson gson = GsonUtil.getGsonWithAnnotation();
		String nodeList = gson.toJson(list);
		model.addAttribute("nodes", nodeList);
		model.addAttribute("roleid", uid);
		return "sys/role/function";
	}
	
	/**
	 * <b>作者:</b> zrb<br/>
	 * <b>修改时间：</b>2016年1月11日 - 下午5:29:27<br/>
	 * <b>功能说明：</b>	更新用户权限表<br/>
	 * @param rid
	 * @param mids
	 * @param model
	 * @return
	 */
	@RequestMapping("mfun")
	public void manageFunction(Integer uid, String mids, Model model, HttpServletResponse response){
		SysUserMenuExample rmEx = new SysUserMenuExample();
		rmEx.createCriteria().andUserIdEqualTo(uid);
		List<SysUserMenu> rmList = this.sysUserMenuService.list(rmEx);
		List<Integer> ids = new ArrayList<>();
		for(SysUserMenu rm : rmList){
			ids.add(rm.getId());
		}
		List<SysUserMenu> list = new ArrayList<SysUserMenu>();
		String[] midArray = mids.split(",");
		for(String midstr : midArray){
			SysUserMenu menu = new SysUserMenu();
			menu.setUserId(uid);
			menu.setMenuId(Integer.valueOf(midstr));
			list.add(menu);
		}
		this.sysUserMenuService.insertBatch(list);
		if(ids.size()>0){
			rmEx.clear();
			rmEx.createCriteria().andUserIdEqualTo(uid).andIdIn(ids);
			this.sysUserMenuService.deleteByExample(rmEx);
		}
		this.writeJson(response, "{\"state\":200}");
	}
	
	@RequestMapping("msg")
	public String msg(Model model){
		model.addAttribute("msg","授权成功！");
		model.addAttribute("redirect","admin/sysuser/list");
		return "message";
	}

}
